import VueRouter from 'vue-router'
import Vue from 'vue'

import Login from '@/components/Login.vue';
import Home from '@/components/Home.vue'

import UploadDemo1 from "@/components/demo/UploadDemo1";
import CardDemo1 from "@/components/demo/CardDemo1";
import TableDemo1 from "@/components/demo/TableDemo1";
import CascadeDemo1 from "@/components/demo/CascadeDemo1";
import CascadeDemo2 from "@/components/demo/CascadeDemo2";
import CascadeDemo3 from "@/components/demo/CascadeDemo3";
import CascadeDemo4 from "@/components/demo/CascadeDemo4";

import VuexDemo1 from '@/components/stateshare/VuexDemo1';
import VuexTest from '@/components/stateshare/VuexTest';

import MapDemo1 from '@/components/thirdpartplugins/baidumap/MapDemo1';
import MapDemo2 from '@/components/thirdpartplugins/baidumap/MapDemo2';

import EchartsDemo1 from '@/components/thirdpartplugins/echarts/EchartsDemo1';

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [
        // 重定向
        { path: '/', redirect: '/login' },
        // 登录组件
        { path: '/login', component: Login },
        { path: '/home',
          component: Home,
          children: [
              // "示例"菜单下的菜单项
              { path: '/upload_demo1', component: UploadDemo1 },
              { path: '/card_demo1', component: CardDemo1 },
              { path: '/table_demo1', component: TableDemo1 },
              { path: '/cascade_demo1', component: CascadeDemo1},
              { path: '/cascade_demo2', component: CascadeDemo2},
              { path: '/cascade_demo3', component: CascadeDemo3},
              { path: '/cascade_demo4', component: CascadeDemo4},
              // "状态管理"菜单下的菜单项
              { path: '/vuex_demo1', component: VuexDemo1 },
              { path: '/vuex_test', component: VuexTest},
              // "第三方常用插件"菜单下的菜单项
              { path: '/map_demo1', component: MapDemo1 },
              { path: '/map_demo2', component: MapDemo2 },
              { path: '/echarts_demo1', component: EchartsDemo1 }
          ]
        }
    ]
})


// 全局导航守卫
router.beforeEach((to, from, next)=>{
    // 如果访问的路由是/login, 不判断直接放行
    if(to.path === '/login')
        return next()
    // 如果访问的路由不是/login, 判断是否有token
    // 从sessionStorage中获取token
    const token = window.sessionStorage.getItem('token')
    if(!token)
        return next('/login')
    next()
})

export default router
